package ua.edu.sumdu.j2se.roman.lab1.view.console;

import ua.edu.sumdu.j2se.roman.lab1.view.AddView;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * Author: Radko Roman.
 */

/**
 * This class responsible for creating console dialog with user on case if need to add task to the list.
 *
 */
public class ConsoleAddView implements AddView {
    /**
     * Contains date format.
     *
     */
    public static final String DATE_PATTERN = "dd.MM.yy h:mm:ss";
    /**
     * Creates for feedback with controller.
     *
     */
    private Scanner scanner;
    /**
     * Contains title of added task.
     * 
     */
    private String titleOfAddedTask;
    /**
     * Contains data of added task.
     * 
     */
    private Date startDateOfAddedTask;
    /**
     * Creates for set format of the receiving date.
     *
     */
    private DateFormat format = new SimpleDateFormat(DATE_PATTERN);


    public ConsoleAddView() {
        scanner = new Scanner(System.in);
    }

    /**
     * Gets from the view title of the added task.
     *
     * @return title of the added task
     */
    @Override
    public Date getStartDateOfAddedTask() {
        return startDateOfAddedTask;
    }
    /**
     * Gets from the view start date of the added task.
     *
     * @return start date of the added task
     */
    @Override
    public String getTitleOfAddedTask() {
        return titleOfAddedTask;
    }
    /**
     * Ask user to enter task parameters, read it from the console, and add task to the list.
     *
     */
    @Override
    public void show() {
        System.out.println("Enter parameters of new task");
        titleOfAddedTask = readTitle();
        startDateOfAddedTask = readDate();
    }

    /**
     * Reads title of the adding task.
     *
     * @return  title
     */
    private String readTitle() {
        System.out.print("       title: ");
        return scanner.nextLine();
    }

    /**
     * Read date of the adding task from console.
     *
     * @return  date of task
     */
    private Date readDate() {
        while (true) {
            System.out.print("       scheduled time in format " + DATE_PATTERN + " : ");
            String time = scanner.nextLine();
            try {
                Date result = format.parse(time);
                return result;
            } catch (ParseException e) {
                System.out.println("Check your date format, try again.");
            }
        }
    }

}
